package com.huafeng.listener;

import cn.hutool.json.JSONUtil;
import com.huafeng.entity.Message;
import com.huafeng.enums.MessageEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * @author 花风
 * @date 2023/4/5 10:39
 * @Description 消息监听器
 */
@Slf4j
@Service
@com.huafeng.annotations.Listener(type = "message")
public class MessageListener implements Listener {
    @Override
    public Object parse(String messageJson) {
        Message message = JSONUtil.toBean(messageJson, Message.class);
        String messageType = message.getMessageType();
        //根据消息类型获取对应的处理函数
        Function<List<String>, Object> function = MessageEnum.get(messageType);
        //看是否是群聊还是私聊
        String id = message.getGroupId() != null ? message.getGroupId() : message.getUserId();
        function.apply(Arrays.asList(id,message.getNickname(),message.getMessage()));
        return null;
    }
}
